package de.koller.occultation.engine.worldGen;

import de.koller.aframe.util.misc.ARangeD;
import de.koller.aframe.util.misc.BufferedRandomFloat;
import de.koller.aframe.util.noise.n2D.Perlin2D;
import de.koller.aframe.util.noise.n2D.ScrollingPerlin2D;
import java.util.Random;

public class WorldGen {
	
	private final Random rnd;
	private final TileGenMap map;
	
	private final ARangeD deepWaterRange = new ARangeD( -1, .35 );
	private final ARangeD waterRange = new ARangeD( .25, .49 );
	private final ARangeD sandRange = new ARangeD( .49, .51 );
	private final ARangeD grassRange = new ARangeD( .51, 2 );
	
	private WorldGen( Random rnd, int size ) {
		this.rnd = rnd;
		this.map = new TileGenMap( size, size );
	}
	
	public static TileGenMap createWorld( Random rnd, int size ) {
		WorldGen g = new WorldGen( rnd, size );
		return g.gen();
	}
	
	public TileGenMap gen() {
		BufferedRandomFloat bfr = new BufferedRandomFloat( "temp/worldgen", rnd );
		ScrollingPerlin2D perlin = new ScrollingPerlin2D( bfr, 8, .7f );
		perlin.normalizeMaxBound( Perlin2D.maxHeight( 0, 8, .7f ) );
		
		for( int x = 0; x < map.getWidth(); x++ ) {
			for( int y = 0; y < map.getHeight(); y++ ) {
				float f = perlin.getValue( 1f / map.getWidth() * x * 3, 1f / map.getHeight() * y * 3);
				
				if( deepWaterRange.isIn( f ) ) map.setBaseLayer( TileGenMap.BaseLayer.WaterDeep, x, y );
				if( waterRange.isIn( f ) ) map.setBaseLayer( TileGenMap.BaseLayer.WaterLow, x, y );
				if( sandRange.isIn( f ) ) map.setBaseLayer( TileGenMap.BaseLayer.Sand, x, y );
				if( grassRange.isIn( f ) ) map.setBaseLayer( TileGenMap.BaseLayer.Grass, x, y );
			}
			
			if( x % 10 == 0 )
				System.out.println( (int)(100d / map.getWidth() * x) + "%" );
		}
				
		return map;
	}
	
	public static void main( String[] args ) {
		TileGenMapPreview.show( createWorld( new Random(), 648 ) );
	}
	
}
